import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

///底部信息构建器
typedef Widget AreaBuilder<T>(String user, String info);

///视频流项目
class FlowItem<T> extends StatefulWidget {
  ///默认构造器
  const FlowItem({
    Key key,
    @required this.index,
    this.areaBuilder,
  }) : super(key: key);

  ///自定义构造器
  FlowItem.custom({
    Key key,
    @required this.index,
    this.areaBuilder,
  }) : super(key: key);

  ///数据项索引
  final int index;

  ///下方信息区域
  final AreaBuilder<T> areaBuilder;

  @override
  _FlowItemState createState() => _FlowItemState<T>();
}

class _FlowItemState<T> extends State<FlowItem> {
  ///播放控制器
  VideoPlayerController _controller;

  ///是否正在缓冲
  bool _isBuffering = false;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(
        "https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218114723HDu3hhxqIT.mp4")
      ..initialize()
      ..play().then((value) => () {
            Future.delayed(const Duration(milliseconds: 200), () {
              setState(() {});
            });
          })
      ..addListener(_playListen);
  }

  ///播放监听
  _playListen() {
    // L.d("buf = ${_controller.value.buffered}");
    // L.d("dur = ${_controller.value.duration}");
    // L.d("ply = ${_controller.value.position}");

    if (_isBuffering && !_controller.value.isBuffering) {
      setState(() {
        _isBuffering = false;
      });
    } else if (!_isBuffering && _controller.value.isBuffering) {
      setState(() {
        _isBuffering = true;
      });
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  void setState(fn) {
    if (this.mounted) super.setState(fn);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      color: [Colors.red, Colors.purple][widget.index % 2],
      child: GestureDetector(
        child: Stack(
          children: [
            _buildPlayBtn,
            _buildVideo,
            _buildSpecialEfficacy,
            _buildContent,
            _buildIndicator,
          ],
        ),
      ),
    );
  }

  ///构建视频层
  Widget get _buildVideo {
    return _controller.value.isBuffering
        ? Container()
        : AspectRatio(
            aspectRatio: _controller.value.aspectRatio,
            child: VideoPlayer(_controller),
          );
  }

  ///构建播放按钮
  Widget get _buildPlayBtn {
    return Center(
      child: Container(
        alignment: Alignment.center,
        width: 100,
        height: 100,
        color: Colors.white,
        child: Text('播放按钮'),
      ),
    );
  }

  ///构建进度条
  Widget get _buildIndicator {
    return Align(
      alignment: Alignment.bottomCenter,
      child: Container(
        width: double.infinity,
        height: 3,
        color: Colors.white70,
      ),
    );
  }

  ///构建可操作内容
  Widget get _buildContent {
    return Align(
      alignment: Alignment.bottomCenter,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Expanded(
            child: Container(
              alignment: Alignment.center,
              height: 100,
              color: Colors.white.withOpacity(0.5),
              child: Text('信息区域'),
            ),
          ),
          Container(
            width: 80,
            height: 450,
            alignment: Alignment.center,
            color: Colors.white38,
            child: Text('操作区域'),
          ),
        ],
      ),
    );
  }

  ///构建特效层
  Widget get _buildSpecialEfficacy {
    return Container();
  }
}
